﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel.Composition;
using Core.DataAccess.IRepositories;

namespace Core.DataAccess.Repositories
{
    [Export(typeof(IFriendInvitationRepository))]
    public class FriendInvitationRepository : IFriendInvitationRepository
    {
        private Connection conn;

        public FriendInvitationRepository()
        {
            conn = new Connection();
        }

        public void SaveFriendInvitation(FriendsInvitation friendInvitation)
        {
            using (MXHEntities context = conn.GetContext())
            {
                friendInvitation.CreateDate = DateTime.Now;
                context.FriendsInvitations.AddObject(friendInvitation);
                context.SaveChanges();
            }
        }

        public int CountFriendRequestInvitation(int accountID)
        {
            using (MXHEntities context = conn.GetContext())
            {
                return context.FriendsInvitations.Where(n => n.BecameAccountID == accountID).Select(n=>n.AccountID).Distinct().Count();
            }
        }

        public List<Int32> GetFriendsInvitationIDByBecameID(Int32 BecameFriendID)
        {
            List<Int32> result = new List<Int32>();
            using (MXHEntities context = conn.GetContext())
            {
                IEnumerable<Int32> friendsInvitationIDs = (from n in context.FriendsInvitations
                                                         where n.BecameAccountID == BecameFriendID
                                                         select n.AccountID
                                                         ).Distinct();
                result = friendsInvitationIDs.ToList();
            }
            return result;
        }


        public void CleanUpFriendInvitionByBecameID(int becameID)
        {
            using (MXHEntities context = conn.GetContext())
            {
                context.FriendsInvitations.Where(n => n.AccountID == becameID).ToList().ForEach(i => context.FriendsInvitations.DeleteObject(i));
                context.SaveChanges();
            }
        }


        public List<Int32> GetFriendsInvitationIDByAccountID(int AccountID)
        {
            List<Int32> result = new List<Int32>();
            using (MXHEntities context = conn.GetContext())
            {
                IEnumerable<Int32> friendInvitations = context.FriendsInvitations.Where(n => n.AccountID == AccountID).Select(n=>n.BecameAccountID.Value).Distinct();
                result = friendInvitations.ToList();
            }
            return result;
        }


        public List<int> GetAllFriendInvitationBecameAccountID()
        {
            List<int> result = new List<int>();
            using (MXHEntities context = conn.GetContext())
            {
                IEnumerable<int> ids = context.FriendsInvitations.Select(n => n.BecameAccountID.HasValue ? n.BecameAccountID.Value : 0).Distinct();
                result = ids.ToList();
            }
            return result;
        }


        public List<FriendsInvitation> GetListFriendsRequestByAccountID(int accountID)
        {            
            using (MXHEntities context = conn.GetContext())
            {
                var lst = from fr in context.FriendsInvitations
                          where fr.AccountID == accountID
                          select fr;
                return lst.Count() > 0 ? lst.ToList() : null;
            }            
        }
    }
}
